﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;

    public class FileUploads : BasePage
    {
        protected HtmlInputButton btnAdd;
        protected Button btUpFiles;
        protected FileUpload File1;
        protected HtmlInputFile File2;
        protected HtmlInputFile File3;
        protected HtmlInputFile File4;
        protected HtmlInputFile File5;
        protected HtmlInputFile File6;
        protected HtmlInputHidden filepath;
        protected HtmlForm form1;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected HtmlInputHidden height;
        protected Label lblMsg;
        protected Label lblOverrideLocation;
        protected HtmlLink mycss;
        protected HtmlInputHidden originalpath;
        protected HtmlTable Table1;
        protected HtmlInputHidden thumbnailpath;
        protected HtmlInputHidden width;

        protected void btUpFiles_Click(object sender, EventArgs e)
        {
            this.UpLoadFiles();
        }

        private string GetUniquelyString()
        {
            Random rand = new Random();
            return DateTime.Now.ToString("yyyyMMddHHmmssfff");
        }

        public void MakeThumbnail(Stream stream, string thumbnailPath, int width, int height, string mode)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromStream(stream);
            int towidth = width;
            int toheight = height;
            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;
            string CS40000 = mode;
            if ((CS40000 != null) && (CS40000 != "HW"))
            {
                if (!(CS40000 == "W"))
                {
                    if (CS40000 == "H")
                    {
                        towidth = (originalImage.Width * height) / originalImage.Height;
                    }
                    else if (CS40000 == "Cut")
                    {
                        if ((((double) originalImage.Width) / ((double) originalImage.Height)) > (((double) towidth) / ((double) toheight)))
                        {
                            oh = originalImage.Height;
                            ow = (originalImage.Height * towidth) / toheight;
                            y = 0;
                            x = (originalImage.Width - ow) / 2;
                        }
                        else
                        {
                            ow = originalImage.Width;
                            oh = (originalImage.Width * height) / towidth;
                            x = 0;
                            y = (originalImage.Height - oh) / 2;
                        }
                    }
                }
                else
                {
                    toheight = (originalImage.Height * width) / originalImage.Width;
                }
            }
            System.Drawing.Image bitmap = new Bitmap(towidth, toheight);
            Graphics g = Graphics.FromImage(bitmap);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
            try
            {
                MemoryStream temp = new MemoryStream();
                bitmap.Save(temp, ImageFormat.Jpeg);
                using (Stream localFile = new FileStream(thumbnailPath, FileMode.OpenOrCreate))
                {
                    localFile.Write(temp.ToArray(), 0, (int) temp.Length);
                }
            }
            catch (Exception e)
            {
                this.lblMsg.Text = e.Message;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                base.InitPage(this.Page);
                this.width.Value = base.Request.QueryString["width"].ToString();
                this.height.Value = base.Request.QueryString["height"].ToString();
            }
            base.Response.Expires = -1;
            this.lblMsg.Text = "";
        }

        private void UpLoadFiles()
        {
            HttpFileCollection uploadedFiles = base.Request.Files;
            try
            {
                string temp = string.Empty;
                for (int i = 0; i < uploadedFiles.Count; i++)
                {
                    HttpPostedFile userPostedFile = uploadedFiles[i];
                    if (userPostedFile.ContentLength > 0)
                    {
                        string savePath = HttpContext.Current.Request.PhysicalApplicationPath + "Upload/Original/";
                        string date = this.GetUniquelyString();
                        savePath = savePath + date + Path.GetFileName(userPostedFile.FileName);
                        temp = temp + date + Path.GetFileName(userPostedFile.FileName) + ",";
                        if (File.Exists(savePath))
                        {
                            File.Delete(savePath);
                        }
                        userPostedFile.SaveAs(savePath);
                        string thumbnailPath = savePath.Replace("Original", "Thumbnail");
                        this.MakeThumbnail(userPostedFile.InputStream, thumbnailPath, int.Parse(this.width.Value), int.Parse(this.height.Value), "HW");
                    }
                }
                if (temp.Length > 0)
                {
                    this.hdnRtnValue.Value = temp.ToString().Substring(0, temp.ToString().Length - 1);
                }
                else
                {
                    this.hdnRtnValue.Value = "";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "Upload Failed :" + ex.Message;
            }
        }
    }
}

